# @FileName  : conf.py
# @Time      : 2022/3/5 17:18
# @Author    : LuZhaoHui
# @Software  : PyCharm

from configparser import ConfigParser


class MyParser(ConfigParser):
    "Inherit from built-in class: ConfigParser"

    def optionxform(self, optionstr):
        "Rewrite without lower()"
        return optionstr


class ocrConf():
    def __init__(self, file):
        self.initVar()
        self.config = MyParser()
        self.name = file
        self.readConf()

    def initVar(self):
        # 设计配置
        self.varConf = {'OcrPath': '',
                        'Title': '',
                        'OcrConfRightToLeft': 0,
                        'OcrConfUpToDown': 0,
                        'OcrConfUpToDownWordNums': 1,
                        'OcrConfFantiToJianti': 0,
                        'OcrConfWordSpaceChar': '竖杠',
                        'OcrConfPeriodTime': 0.5,
                        'IsTcpServer': 0,
                        'TcpHost': '127.0.0.1',
                        'TcpPort': 8099,
                        'IsOcrWeb': 1,
                        'OcrType': 0,
                        # 0-汉王，1-百度在线
                        'OcrWebHost': '127.0.0.1',
                        'OcrWebPort': 8088,
                        'OcrFileMaxByte': 3000000,
                        'API_KEY': 'wdfxbeltc6jNdVl5DMK0p9uj',
                        'SECRET_KEY': 'KuAp9cHvzCZvpPdwYDUvjAZR7NYtEnew',
                        'IsDb': 0,
                        'DbHost': '127.0.0.1',
                        'DbPort': 3306,
                        'DbUser': 'root',
                        'DbPass': 'root',
                        'DbName': 'ocr',
                        'IsData': 1,
                        'DataHost': '127.0.0.1',
                        'DataPort': 22,
                        'DataUser': 'root',
                        'DataPass': 'Dashen2023',
                        'DataPath': '/home/dashen/image',
                        'Start': 0,
                        'WindowMin': 0,
                        'WindowMax': 0}

    def getConfig(self, section, key):
        return self.config.get(section, key)

    def readConf(self):
        self.config.read(self.name, 'utf-8')
        # [CONF]
        for k in self.varConf:
            str = self.getConfig('CONF', k)
            if isinstance(self.varConf[k], int):
                self.varConf[k] = int(str)
            elif isinstance(self.varConf[k], float):
                self.varConf[k] = float(str)
            else:
                self.varConf[k] = str

    def writeConf(self, key, value):
        self.config.set('CONF', key, str(value))
        with open(self.name, 'w', encoding='utf-8') as f:
            self.config.write(f)
